/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { App } from "vue";
import type { RouteRecordName, RouteRecordRaw } from "vue-router";
import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import {
  authentication,
  base,
  disableRouterWarning,
  isHashRouterMode,
} from "/@/config";
import { setupPermissions } from "/@/router/permissions";
import Layout from "/@vab/layouts/index.vue";
import {TypeUtils} from "/@/utils/TypeUtils.ts";

export const constantRoutes: VabRouteRecord[] = [
  {
    path: "/login",
    name: "Login",
    component: () => import("/@/views/login/Login.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("/@/views/login/Register.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/password",
    name: "Password",
    component: () => import("/@/views/login/Password.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/redirect",
    name: "Redirect",
    component: () => import("/@/views/redirect/Redirect.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/403",
    name: "403",
    component: () => import("/@/views/error/403.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/404",
    name: "404",
    component: () => import("/@/views/error/404.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/500",
    name: "500",
    component: () => import("/@/views/error/500.vue"),
    meta: {
      hidden: true,
    },
  },  {
    path: "/signin",
    name: "signin",
    component: () => import("/@/views/signin/index.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/study",
    name: "study",
    component: () => import("/@/views/study/index.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/exam",
    name: "exam",
    component: () => import("/@/views/exam/index.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/examResult",
    name: "examResult",
    component: () => import("/@/views/examResult/index.vue"),
    meta: {
      hidden: true,
    },
  },
  // {
  //   path: "/",
  //   name: "/",
  //   component: Layout,
  //   meta: {
  //     noColumn:true,
  //         title: "首页",
  //         icon: "home-2-line",
  //   },
  //      children: [
  //        {
  //          path: "/",
  //          name: "index",
  //          component: () => import("/@/views/welcome/index.vue"),
  //          meta: {
  //            title: "欢迎页",
  //             icon: "home-2-line",
  //              noClosable: false,
  //              noKeepAlive: true
  //          },
  //        },
  //      ],
  // },
];


const router = createRouter({
  history: isHashRouterMode
    ? createWebHashHistory(base)
    : createWebHistory(base),
  routes: constantRoutes as RouteRecordRaw[],
});

export const addRouter = (routes: VabRouteRecord[]) => {
  try {
    for (let route of routes) {
      let routeName = route.name;
      if (!routeName) {
        continue;
      }
      let hasRoute = router.hasRoute(routeName);
      if (!hasRoute) {
        router.addRoute(route as RouteRecordRaw);
      }
      if (route.children) addRouter(route.children);

    }
  } catch (e) {
    console.log(e);
  }

};

export const setupRouter = (app: App<Element>) => {
  router.addRoute({
    path: "/:pathMatch(.*)*",
    component: () => import("/@/views/error/404.vue"),
  });
  setupPermissions(router);
  app.use(router);

  return router;
};

export default router;
